﻿
games.game12 = {
    //snakes
    init: function () {
        $("#gamezone").html("<p style='font-size:18pt;padding:30px;'><strong style='font-size:20pt;'>Snakes</strong><br/>Use <span style='color:#933'>up</span>, <span style='color:#939'>down</span>, <span style='color:#339'>left</span> and <span style='color:#393'>right</span> to control the paddle</p><canvas id='game12-snakes' width='400' height='400' style='border:1px solid #000;margin:50px 50px;float:right;'></canvas>");
        this.breakout($("#game12-snakes"), 400, 400);
    },
    breakout: function (element, h, w) {
        engine = {
            h: 0, w: 0,
            gameBoard: undefined,
            keysDown: new Array(),
            engineloop: undefined,
            keyloop: undefined,
            init: function (gameBoard, h, w) {
                this.gameBoard = gameBoard;
                this.h = h;
                this.w = w;
                var that = this;
                this.world.init(that, this.h, this.w, function () {
                    clearInterval(that.engineloop);
                    clearInterval(that.keyloop);
                });
                document.onkeydown = function (e) {
                    var KeyID = (window.event) ? event.keyCode : e.keyCode;
                    return that.handleKeyDown(KeyID);
                }
                document.onkeyup = function (e) {
                    var KeyID = (window.event) ? event.keyCode : e.keyCode;
                    return that.handleKeyUp(KeyID);
                }
                alert("are you ready... left and right");
                this.engineloop = setInterval(function () {
                    that.tick();
                }, 100);
                this.keyloop = setInterval(function () {
                    that.handleKeys();
                }, 1);
            },
            handleKeys: function () {
                if (this.keysDown[39]) {
                    this.world.internalPlayer.moveRight();
                }
                if (this.keysDown[37]) {
                    this.world.internalPlayer.moveLeft();
                }
                if (this.keysDown[40]) {
                    this.world.internalPlayer.moveDown();
                }
                if (this.keysDown[38]) {
                    this.world.internalPlayer.moveUp();
                }
            },
            handleKeyUp: function (keyId) {
                this.keysDown[keyId] = false;
                return !(keyId == 37 | keyId == 39 | keyId == 38 | keyId == 40);
            },
            handleKeyDown: function (keyId) {
                this.keysDown[keyId] = true;
                return !(keyId == 37 | keyId == 39 | keyId == 38 | keyId == 40);
            },
            player: {
                limbs: [{ x: 0, y: 390, color: '#fff'}],
                x: 0,
                y: 390,
                h: 10,
                w: 10,
                l: 5,
                max: 0, min: 0,
                speed: 2,
                facing: 'e',
                score: 0,
                moveRight: function () { this.facing = this.facing == 'w' ? 'w' : 'e'; },
                moveLeft: function () { this.facing = this.facing == 'e' ? 'e' : 'w'; },
                moveUp: function () { this.facing = this.facing == 's' ? 's' : 'n'; },
                moveDown: function () { this.facing = this.facing == 'n' ? 'n' : 's'; },
                grow: function (growBy, newx, newy) {
                    var letters = '0369cf'.split('');
                    var color = '#';
                    for (var i = 0; i < 3; i++) {
                        color += letters[Math.round(Math.random() * 6)];
                    }
                    this.limbs[this.limbs.length] = { x: newx, y: newy, color: color };
                },
                render: function (ctx) {
                    ctx.fillStyle = '#000';
                    ctx.font = "bold 150px sans-serif";
                    ctx.fillText(this.score, 150, 225);
                    ctx.strokeColor = '#000';
                    for (l in this.limbs) {
                        ctx.fillStyle = this.limbs[l].color;
                        ctx.fillRect(this.limbs[l].x, this.limbs[l].y, this.w, this.h);
                        ctx.strokeRect(this.limbs[l].x, this.limbs[l].y, this.w, this.h);
                    }
                }
            },
            target: {
                x: undefined,
                y: undefined,
                h: 10,
                w: 10,
                init: function (x, y) {
                    this.x = x;
                    this.y = y;
                },
                color: '#f00',
                render: function (ctx) {
                    if (this.x == undefined && this.y == undefined) { return; }
                    ctx.fillStyle = this.color;
                    ctx.fillRect(this.x, this.y, this.w, this.h);
                }
            },
            world: {
                internalPlayer: undefined,
                internalApple: undefined,
                h: 0, w: 0,
                killCallback: undefined,
                init: function (that, h, w, killCallback) {
                    this.h = h;
                    this.w = w;
                    this.killCallback = killCallback;
                    this.internalPlayer = that.player;
                    this.internalApple = that.target;
                    this.internalApple.init(games.helper.getRandomNumber(0, this.w / 10) * 10, games.helper.getRandomNumber(0, this.h / 10) * 10);
                    this.internalPlayer.max = h;
                    this.internalPlayer.min = 0;
                },
                render: function (ctx) {
                    ctx.clearRect(0, 0, this.h, this.w);
                    this.internalPlayer.render(ctx);
                    this.internalApple.render(ctx);
                },
                adjust: function () {
                    var tailx = this.internalPlayer.limbs[this.internalPlayer.limbs.length - 1].x;
                    var taily = this.internalPlayer.limbs[this.internalPlayer.limbs.length - 1].y;

                    var headx = this.internalPlayer.limbs[0].x;
                    var heady = this.internalPlayer.limbs[0].y;
                    switch (this.internalPlayer.facing) {
                        case 'n':
                            this.internalPlayer.limbs[0].y -= this.internalPlayer.h;
                            break;
                        case 's':
                            this.internalPlayer.limbs[0].y += this.internalPlayer.h;
                            break;
                        case 'e':
                            this.internalPlayer.limbs[0].x += this.internalPlayer.w;
                            break;
                        case 'w':
                            this.internalPlayer.limbs[0].x -= this.internalPlayer.w;
                            break;
                    }
                    for (var i = 1; i < this.internalPlayer.limbs.length; i++) {
                        var headxtemp = this.internalPlayer.limbs[i].x;
                        var headytemp = this.internalPlayer.limbs[i].y;
                        if (headxtemp == this.internalApple.x && headytemp == this.internalApple.y) {
                            this.internalApple.init(games.helper.getRandomNumber(0, this.w / 10) * 10, games.helper.getRandomNumber(0, this.h / 10) * 10);
                            this.internalPlayer.l += 4;
                        }
                        if (this.internalPlayer.limbs[0].x == headxtemp && this.internalPlayer.limbs[0].y == headytemp && i > 2) {
                            if (this.killCallback != undefined) {
                                this.killCallback();
                            }
                            alert("Uh oh, you died!");
                            return false;
                        }
                        this.internalPlayer.limbs[i].x = headx;
                        this.internalPlayer.limbs[i].y = heady;
                        headx = headxtemp;
                        heady = headytemp;

                    }
                    if (this.internalPlayer.limbs[0].x == this.internalApple.x && this.internalPlayer.limbs[0].y == this.internalApple.y) {
                        this.internalApple.init(games.helper.getRandomNumber(0, this.w / 10) * 10, games.helper.getRandomNumber(0, this.h / 10) * 10);
                        this.internalPlayer.l += 4;
                        this.internalPlayer.score++;
                    }
                    if (this.internalPlayer.l > 0) {
                        this.internalPlayer.l--;
                        this.internalPlayer.grow(1, tailx, taily);
                    }
                    if (this.internalPlayer.limbs[0].x > this.w - this.internalPlayer.w || this.internalPlayer.limbs[0].y > this.h - this.internalPlayer.h || this.internalPlayer.limbs[0].x < 0 || this.internalPlayer.limbs[0].y < 0) {
                        if (this.killCallback != undefined) {
                            this.killCallback();
                        }
                        alert("Uh oh, you died!");
                        return false;
                    }
                    return true;
                }
            },
            tick: function () {
                if (this.world.adjust())
                    this.world.render(this.gameBoard);
            }
        }

        gameBoard = element[0].getContext('2d');
        engine.init(gameBoard, h, w);
    }
};